package com.jlkf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.DriverTurnoverMapper;
import com.jlkf.dao.OrderItemsMapper;
import com.jlkf.pojo.DriverTurnover;
import com.jlkf.service.FinanceService;
import com.jlkf.temp.Page;
import com.jlkf.view.OA.OANoSettlementDetail;
import com.jlkf.view.OA.OAOrderItems;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.DateUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private DriverTurnoverMapper driverTurnoverMapper;
    @Autowired
    private OrderItemsMapper orderItemsMapper;

    //月结算订单
    public Object monthOrders(DriverTurnover month, Page page) {
        ReturnData result = new ReturnData(null);
        
//     if(month.getMonths()!=null){
//    	 String mo=DateUtil.format(month.getMonths(), "yyyy-MM");
//        	month.setMonths(DateUtil.parseDate(month.getMonths(), "yyyy-MM"));
//        }
        if(month.getMonths()!=null&&"".equals(month.getMonths())){
        	month.setMonths(null);
        }else{
        	if(month.getMonths()!=null){
        		String[] months=month.getMonths().split("/");
            	month.setMonths(months[1]+"-"+months[0]);
        	}
        }
        if(month.getYear()!=null&&month.getMou()!=null){
        	month.setMonths(month.getYear()+"-"+(month.getMou()<10?"0"+month.getMou():month.getMou()));
        }
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        List<Map> list = driverTurnoverMapper.monthOrders(month);
        PageInfo<Map> info = new PageInfo<>(list);
        result.setPageNo(info.getPageNum());
        result.setTotalPage(info.getPages());
        result.setTotalRecord((int) info.getTotal());
        result.setData(list);
        return result;
    }

    //月订单
    public Object weekOrders(DriverTurnover week, Page page) {
        ReturnData result = new ReturnData(null);
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        List<Map> list = driverTurnoverMapper.weekOrders(week);
        PageInfo<Map> info = new PageInfo<>(list);
        result.setPageNo(info.getPageNum());
        result.setTotalPage(info.getPages());
        result.setTotalRecord((int) info.getTotal());
        result.setData(list);
        return result;
    }

    @Override
    public Object monthInfo(DriverTurnover info, Page page) {
    	String date = info.getMonths();
    String[] dats=date.split("-");
    	Map<String,Object> map1 = new HashMap<String, Object>();
    	Calendar cal = Calendar.getInstance();  
        cal.setTime(DateUtil.parseDate(info.getMonths()));  
    	Date d=cal.getTime();
    	String mon=(d.getMonth()+1)<10?"0"+(d.getMonth()+1):(d.getMonth()+1)+"";
    	 map1.put("startTime",dats[0]+"-"+mon);//结算时间开始时间
//		 map1.put("endTime",dats[0]+"-"+mon+"-31");//结算时间 结束时间
		 map1.put("driverId", info.getDriverId());
		 PageHelper.startPage(page.getPageNo(),page.getPageSize());
		 List<OANoSettlementDetail> list=orderItemsMapper.detailsOfSettlementOrder(map1);
		 PageInfo<OANoSettlementDetail> infos = new PageInfo<>(list);
		  return new ReturnData(list,(int)infos.getPages(),(int)infos.getTotal(),200,"success");
    }

    @Override
    public Object weekInfo(DriverTurnover info, Page page) {
    	Date date = info.getMonth();
    	Map<String,Object> map1 = new HashMap<String, Object>();
    	 map1.put("startTimes",DateUtil.format(geLastWeekMonday(date), "yyyy-MM-dd"));//结算时间开始时间
		map1.put("endTimes",DateUtil.format(geLendWeekMonday(geLastWeekMonday(date)), "yyyy-MM-dd"));//结算时间 结束时间
		map1.put("driverId", info.getDriverId());
		 PageHelper.startPage(page.getPageNo(),page.getPageSize());
		 List<OANoSettlementDetail> list=orderItemsMapper.detailsOfSettlementOrder(map1);
		 PageInfo<OANoSettlementDetail> infos = new PageInfo<>(list);
		  return new ReturnData(list,(int)infos.getPages(),(int)infos.getTotal(),200,"success");
    }

    //获取上周周一
    public static Date geLastWeekMonday(Date date) {  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(getThisWeekMonday(date));  
        cal.add(Calendar.DATE, -7);  
        return cal.getTime();  
    } 
    //给定时间  本周结束时间
    public static Date geLendWeekMonday(Date date) {  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(date);  
        cal.add(Calendar.DATE, 7);  
        return cal.getTime();  
    } 
    public static Date getThisWeekMonday(Date date) {  
        Calendar cal = Calendar.getInstance();  
        cal.setTime(date);  
        // 获得当前日期是一个星期的第几天  
        int day = cal.get(Calendar.DAY_OF_WEEK);  
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值  
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);  
        return cal.getTime();  
    }  
}
